#
# Classifier for Postfix/Vmailer bounce messages
#
package Mail::Bounce::Postfix;

use strict;
use vars qw(@ISA @patterns %statusmap);

@ISA = qw(Mail::Bounce::Heuristic);

# reason for MTATYPE:
# normally Postfix.
# At least one install uses Sendmail here instead.

$patterns[0] = << 'EoF';
This is the ${MTATYPE-.+} program at host ${MTA-\S+}.

I'm sorry to have to inform you that the message returned
below could not be delivered to one or more destinations.

For further assistance, please contact <${POSTMASTER-\S+}>

If you do so, please include this problem report. You can
delete your own text from the message returned below.

			The ${MTATYPE2-.+} program

<${FINAL_RECIPIENT-\S+}>: ${DIAGNOSTIC-(?s:.*)}

EoF
# note - diagnostic can cover multiple lines, but MUST end with a blank line.

$patterns[1] = << 'EoF';
This is an automated reply from the system ${MTA-\S+}.

I'm sorry to inform you that the message returned
below could not be delivered to one or more destinations.

For further assistance, please contact <${POSTMASTER-\S+}>

If you do so, please include this problem report. You can
delete your own text from the message returned below.

Here's a diagnostic clue:

<${FINAL_RECIPIENT-\S+}>: ${DIAGNOSTIC-(?s:.*)}

EoF

# pattern from mx2.netease.com
# JUNK seemed to be an intro in Russian, or something.

$patterns[2] = << 'EoF';
This is the ${MTATYPE-.+} program at host ${MTA-\S+}.

I'm sorry to have to inform you that the message returned
below could not be delivered to one or more destinations.

For further assistance, please contact <${POSTMASTER-\S+}>

If you do so, please include this problem report. You can
delete your own text from the message returned below.

			The ${MTATYPE2-.+} program

${JUNK-(?s:.+)}

<${FINAL_RECIPIENT-\S+}>: ${DIAGNOSTIC-(?s:.*)}

EoF

# pattern based on cirrus.netspace.net.au
# He modified heavily the stuff between "This is the" and "The Postfix program"
$patterns[3] = << 'EoF';
This is the ${MTATYPE-.+} program at host ${MTA-\S+}.


I'm sorry to have to inform you that ${-(?s:.+)}

For further assistance, please contact <${POSTMASTER-\S+}>

If you do so, please include ${-(?s:.+)}

			The ${MTATYPE2-.+} program

<${FINAL_RECIPIENT-\S+}>: ${DIAGNOSTIC-(?s:.*)}

EoF

# Pattern based on smtp2.minitel.net
# He included a header before "This is the".
$patterns[4] = << 'EoF';
${-(?s:.*)}
This is the ${MTATYPE-.+} program at host ${MTA-\S+}.

I'm sorry to have to inform you that ${-(?s:.+)}

For further assistance, please contact <${POSTMASTER-\S+}>

If you do so, please include ${-(?s:.+)}

			The ${-.+} program

<${FINAL_RECIPIENT-\S+}>: ${DIAGNOSTIC-(?s:.*)}

EoF

# Pattern based on ucalgary.ca.
# Kind of "the ultimate fallback" for Postfix-style responses.

$patterns[5] = << 'EoF';
${JUNK-(?s:.*)}

<${FINAL_RECIPIENT-\S+}>: ${DIAGNOSTIC-(?s:.*)}
EoF

# Status mappings
%statusmap = (
"unknown user" => "5.1.1 (Unknown user)",
);

sub validate {
    my $self = shift;
    my $keywords = $self->patternmatch($self->firstbodystring, @patterns);
    return defined($keywords);
}

    

sub recipients {
    my $self = shift;

    if (wantarray()) {
	my $res;
	my $keywords = $self->patternmatch($self->firstbodystring,
					   @patterns);
	if (defined($keywords)) {
	    $res = new Mail::Bounce::Address;
	    $res->type("permfail");
	    $res->action("failed");
	    if ($$keywords{DIAGNOSTIC} =~ /^unknown user/) {
		$res->status("5.1.1 (Unknown user)");
	    } else {
		$res->status("5.0.0");
		$res->diagnostic($$keywords{DIAGNOSTIC});
	    }
	    $res->final_recipient($$keywords{FINAL_RECIPIENT});
	} else {
	    die "Postfix: Unmatched pattern in recipients()\n";
	}
	return $res;
    } else {
	return 1;
    }
}
1;




